/*******************************************************************
 *
 * Copyright © 2008 - 2009 All Rights Reserved. 
 *
 * @Version : 1.0 
 *
 * @Filename PropertyTools.java
 *
 * @author wangyongdong
 *
 * @Eamil wangyongdong@webgame.com.cn
 *
 * @Date  2009-10-27 上午11:32:04
 *
 *******************************************************************/
package game.breeze.util;

import java.util.List;
import java.util.Queue;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.log4j.Logger;



/**
 * @author wangyongdong
 * 
 */
public class PropertyTools {

	private final static Logger LOGGER = Logger.getLogger(PropertyTools.class);

	/**
	 *错误码
	 */
	public static final int ERROR_NUMBER = -999999999;

	/**
	 * 返回对象的指定属性值
	 * 如果错误返回错误码 -999999999 
	 * @param object
	 * @param property
	 * @return MonsterBattleAIProcessor.java MonsterBattleAIProcessor getBenInterProperty int
	 */
	public static int getBenInterProperty(Object object, String property) {
		try {
			return MathTools.getInteger(BeanUtils.getProperty(object, property));

		} catch (Exception e) {

			LOGGER.error("没有在对象里面找到相应的属性" + " object=" + object + " property=" + property);
			return PropertyTools.ERROR_NUMBER;
		}

	}

	/**
	 * 返回对象的指定属性值
	 * 
	 * @param object
	 * @param property
	 * @return MonsterBattleAIProcessor.java MonsterBattleAIProcessor getBenInterProperty int
	 */
	public static void setBenInterProperty(Object object, String property, Object propertyValue) {
		try {
			BeanUtils.setProperty(object, property, propertyValue);

		} catch (Exception e) {

			LOGGER.error("没有在对象里面找到相应的属性" + " object=" + object + " property=" + property);

		}

	}

	/**
	 * 取指定属性最小一个对象
	 * 
	 * @param userList
	 * @return PropertyTools.java PropertyTools getMinBlood User
	 */
	public static Object getMin(List<Object> list, String property) {
		Object ret = null;
		int now = 0;
		for (Object object : list) {
			if (ret == null) {
				ret = object;
				now = PropertyTools.getBenInterProperty(object, property);
				if (now == PropertyTools.ERROR_NUMBER) {
					return null;
				}
			} else {

				int next = PropertyTools.getBenInterProperty(object, property);

				if (next != PropertyTools.ERROR_NUMBER && next < now) {
					now = next;
					ret = object;
				}
			}
		}
		return ret;
	}

	/**
	 * 取指定属性最大一个对象
	 * 
	 * @param userList
	 * @return PropertyTools.java PropertyTools getMinBlood User
	 */
	public static Object getMax(List<Object> list, String property) {
		Object ret = null;
		int now = 0;
		for (Object object : list) {
			if (ret == null) {
				ret = object;
				now = PropertyTools.getBenInterProperty(object, property);
				if (now == PropertyTools.ERROR_NUMBER) {
					return null;
				}
			} else {

				int next = PropertyTools.getBenInterProperty(object, property);

				if ((next != PropertyTools.ERROR_NUMBER) && next > now) {
					now = next;
					ret = object;
				}
			}
		}
		return ret;
	}

	/**
	 * 取指定属性最小一个对象
	 * 
	 * @param userList
	 * @return PropertyTools.java PropertyTools getMinBlood User
	 */
	public static Object getMin(Queue<Object> queue, String property) {
		Object ret = null;
		int now = 0;
		for (Object object : queue) {
			if (ret == null) {
				ret = object;
				now = PropertyTools.getBenInterProperty(object, property);
				if (now == PropertyTools.ERROR_NUMBER) {
					return null;
				}
			} else {

				int next = PropertyTools.getBenInterProperty(object, property);

				if (next != PropertyTools.ERROR_NUMBER && next < now) {
					now = next;
					ret = object;
				}
			}
		}
		return ret;
	}

	/**
	 * 取指定属性最大一个对象
	 * 
	 * @param userList
	 * @return PropertyTools.java PropertyTools getMinBlood User
	 */
	public static Object getMax(Queue<Object> queue, String property) {
		Object ret = null;
		int now = 0;
		for (Object object : queue) {
			if (ret == null) {
				ret = object;
				now = PropertyTools.getBenInterProperty(object, property);
				if (now == PropertyTools.ERROR_NUMBER) {
					return null;
				}
			} else {

				int next = PropertyTools.getBenInterProperty(object, property);

				if ((next != PropertyTools.ERROR_NUMBER) && next > now) {
					now = next;
					ret = object;
				}
			}
		}
		return ret;
	}
}
